package com.ia.aistream.manager.operating.util.convert;

import com.alibaba.fastjson.JSONObject;
import com.ia.aistream.common.utils.StringUtils;
import com.ia.aistream.common.utils.spring.SpringUtils;
import com.ia.aistream.mapper.system.SysDictDataMapper;
import com.ia.aistream.model.operating.dto.ColumnStyle;
import com.ia.aistream.model.operating.dto.ParagraphTextStyle;
import com.ia.aistream.model.operating.dto.TextInfo;
import com.ia.aistream.model.operating.dto.convert.EquOperInfo;
import com.ia.aistream.model.system.entity.SysDictData;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Ymj
 */
public class ShowSpeicalUtil {

    private static String paramColor = "#f79696";
    private static String logicOpColor = "#8baede";
    private static String equipColor = "#73d473";
    private static String opColor = "yellow";


//    public static void main(String[] args) {
//        String origin = "大于846m3：机组功率运行不受限制";
////        String origin = "在KIC上检测001/002PO的泵轴承温度不超过110℃：";
//        List<SystemEquInfo> list = new ArrayList<>();
//        SystemEquInfo systemEquInfo = new SystemEquInfo();
//        systemEquInfo.setSystem("016VD");
//        list.add(systemEquInfo);
//
//        EquOperInfo equOperInfo = new EquOperInfo();
////        equOperInfo.setOp("流量");
//        equOperInfo.setLogicOp("大于");
//        equOperInfo.setParam("846m3");
//        equOperInfo.setEqSystemList(list);
//        show(origin, null, equOperInfo, "table");
//    }

    /***
     * 处理信息中需要特殊标识的文字
     * @param origin
     * @param jsonObject
     * @param operInfo
     * @param type
     * @return
     */
    public static JSONObject show2(String origin, JSONObject jsonObject, EquOperInfo operInfo, String type){
        // 记录设备信息
        //List<String> eqStr = new ArrayList<>();
        List<TextInfo> textInfoList = new ArrayList<>();
        int num=0;
        // 遍历数据


        /** 处理特殊含义的数据 **/
//        // 获取所有的设备信息
//        List<SystemEquInfo> eqSystemList = operInfo.getEqSystemList();
//        if(eqSystemList!=null){
//            for (SystemEquInfo eq: eqSystemList){
//                eqStr.add(eq.toString());
//            }
//        }
        // 操作数据
        String op = operInfo.getOp();
        // 获取逻辑运算符
        String logicOp = operInfo.getLogicOp();
        // 获取参数信息
        String param = operInfo.getParam();

        List<TextInfo> textInfos = handleTxtInfo(textInfoList, op, logicOp, param);
//        if(eqStr.size()>0){
//            for (String str: eqStr){
//                if(origin.contains(str)){
//                    System.out.println("数据=" + str);
//                }
//            }
//        }
//        List<Integer> ddd = ddd(origin, op, logicOp, param);
//        Collections.sort(ddd);
//        System.out.println(ddd);
//
//        for (Integer index: ddd){
//            int i = index / 10;
//            int location = i - num;
//            int typeNum = index%10;
//            String substring = origin.substring(0, location);
//            if(!StringUtils.isEmpty(substring)){
//                textInfoList.add(creTextInfo(substring, null));
//            }
//            switch (typeNum){
//                case 1:
//                    textInfoList.add(creTextInfo(op, opColor));
//                    num = i + op.length();
//                    origin = origin.substring(location+op.length());
//                    System.out.println(num + origin);
//                    break;
//                case 2:
//                    textInfoList.add(creTextInfo(logicOp, logicOpColor));
//                    num = i + logicOp.length();
//                    origin = origin.substring(location + logicOp.length());
//                    System.out.println(num + origin);
//                    break;
//                case 3:
//                    textInfoList.add(creTextInfo(param, paramColor));
//                    num = i + param.length();
//                    origin = origin.substring(location + param.length());
//                    System.out.println(num + origin);
//                    break;
//                case 4:
//                    break;
//                default:
//                    break;
//            }
//        }
//////        // 处理操作信息
//        if(op!=null && origin.contains(op)){
//            int i = origin.indexOf(op);
//            // 不需要处理的数据
//            String substring = origin.substring(0, i);
//            if(!substring.contains(logicOp) && !substring.contains(param)){
//                textInfoList.add(creTextInfo(substring, null));
//            }else {
//                if(substring.contains(logicOp)){
//
//                }
//            }
//
//            origin = origin.substring(i+op.length());
//            textInfoList.add(creTextInfo(op, opColor));
//        }
//
//        // 处理逻辑运算信息
//        if (logicOp!=null && origin.contains(logicOp)){
//            int i = origin.indexOf(logicOp);
//            // 不需要处理的数据
//            String substring = origin.substring(0, i);
//            textInfoList.add(creTextInfo(substring, null));
//            origin = origin.substring(i+logicOp.length());
//            textInfoList.add(creTextInfo(logicOp, logicOpColor));
//        }
//        // 处理参数信息
//        if (param!=null && origin.contains(param)){
//            int i = origin.indexOf(param);
//            // 不需要处理的数据
//            String substring = origin.substring(0, i);
//            textInfoList.add(creTextInfo(substring, null));
//            origin = origin.substring(i+param.length());
//            param = param.replace("m3", "m<sup style='font-size:1px'>3</sup>");
//            textInfoList.add(creTextInfo(param, paramColor));
//        }
//        if (origin.length()>0){
//            textInfoList.add(creTextInfo(origin, null));
//        }
//
        if("table".equals(type)){
            ColumnStyle columnStyle = jsonObject.toJavaObject(ColumnStyle.class);
            columnStyle.setTextInfoList(textInfoList);
            jsonObject = (JSONObject) JSONObject.toJSON(columnStyle);
        }else{
            ParagraphTextStyle paragraphTextStyle = jsonObject.toJavaObject(ParagraphTextStyle.class);
            paragraphTextStyle.setTextInfoList(textInfoList);
            jsonObject = (JSONObject) JSONObject.toJSON(paragraphTextStyle);
        }

        System.out.println(textInfoList);
        return jsonObject;
    }

    public static JSONObject show(String origin, JSONObject jsonObject, EquOperInfo operInfo, String type){

        // 操作数据
        String op = operInfo.getOp();
        // 获取逻辑运算符
        String logicOp = operInfo.getLogicOp();
        // 获取参数信息
        String param = operInfo.getParam();



        if("table".equals(type)){
            ColumnStyle columnStyle = jsonObject.toJavaObject(ColumnStyle.class);
            List<TextInfo> textInfos = handleTxtInfo(columnStyle.getTextInfoList(), op, logicOp, param);
            columnStyle.setTextInfoList(textInfos);
            jsonObject = (JSONObject) JSONObject.toJSON(columnStyle);
        }else{
            ParagraphTextStyle paragraphTextStyle = jsonObject.toJavaObject(ParagraphTextStyle.class);
            List<TextInfo> textInfos = handleTxtInfo(paragraphTextStyle.getTextInfoList(), op, logicOp, param);
            paragraphTextStyle.setTextInfoList(textInfos);
            jsonObject = (JSONObject) JSONObject.toJSON(paragraphTextStyle);
        }

        return jsonObject;
    }

    /***
     * 实现对象赋值
     * @param content
     * @param color
     * @return
     */
    public static TextInfo creTextInfo(String content, String color){
        TextInfo textInfo = new TextInfo();
        textInfo.setFontSize(16.0f);
        textInfo.setFontName("宋体");
        textInfo.setContent(content);
        if(!StringUtils.isEmpty(color)){
            textInfo.setBgColor(color);
        }
        textInfo.setBold(false);
        textInfo.setFontColor("#000000");

        return textInfo;
    }


    /***
     * 实现数据圈定
     * @param
     */
    public static List<Integer> ddd(String origin, String op, String logicOp, String param){

        List<Integer> list = new ArrayList<>();

        int i=0;
        int opIndex=0;
        int logicIndex = 0;
        int paramIndex = 0;
        boolean opFlag = true;
        boolean logicFlag = true;
        boolean paramFlag = true;

        List<String> equip = getEquip(origin);
        while (i<origin.length()){
            // 处理位置
            if(opFlag && op!=null && origin.contains(op)){
                int indexOf = origin.indexOf(op, opIndex);
                if(indexOf>=0){
                    list.add((indexOf*10+1));
                    opIndex=indexOf+op.length();
                }else{
                    opFlag = false;
                }
            }else if(logicFlag && logicOp!=null && origin.contains(logicOp)){
                int indexOf = origin.indexOf(logicOp, logicIndex);
                if(indexOf>=0){
                    list.add((indexOf*10+2));
                    logicIndex=indexOf+logicOp.length();
                }else {
                    logicFlag = false;
                }
            }else if(paramFlag && param!=null && origin.contains(param)){
                int indexOf = origin.indexOf(param, paramIndex);
                if(indexOf>=0){
                    list.add((indexOf*10+3));
                    paramIndex = indexOf+param.length();
                }else {
                    paramFlag = false;
                }
            }else {
                return list;
            }
            i++;
        }

        int eIndex = 0;
        for (String str: equip){
            if (origin.contains(str)){
                int i1 = origin.indexOf(str, eIndex);
                if(i1>=0){
                    list.add(i1*10+4);
                    eIndex++;
                }else{
                    continue;
                }
            }
        }
        return list;
    }

    /****
     * 获取文件中的设备
     * @param msg
     * @return
     */
    public static List<String> getEquip(String msg){
        List<String> list = new ArrayList<>();
        String reg = "\\d?[A-Z]*\\d{3,5}[A-Z]{1}[a-zA-Z]+|[A-Za-z/0-9]*\\d{3,5}/\\d{3,5}[A-Z]{1}[a-zA-Z]+";
        Pattern pattern = Pattern.compile(reg);
        boolean flag = true;
        while (flag){
            Matcher matcher = pattern.matcher(msg);
            if(matcher.find()){
                String group = matcher.group();
                list.add(group);
                msg = msg.substring(msg.indexOf(group)+group.length());
            }else{
                flag = false;
            }
        }
        return list;
    }

    /***
     * 实现信息处理
     * @return
     */
    public static List<TextInfo> handleTxtInfo(List<TextInfo> list, String op, String logicOP, String param){
        String content = "";
        Float fontSize = 12.0f;
        String fontName = "宋体";
        List<TextInfo> result = new ArrayList<>();
        for (int i=0; i<list.size(); i++){
            TextInfo textInfo = list.get(i);
            String listContent = textInfo.getListContent();

            if(!StringUtils.isEmpty(textInfo.getFontName())){
                fontName = textInfo.getFontName();
            }
            if(textInfo.getFontSize()!=null && textInfo.getFontSize()>0){
                fontSize = textInfo.getFontSize();
            }
            if(!StringUtils.isEmpty(listContent)){
                if(!StringUtils.isEmpty(content)){
                    content = convertColor(content, op, logicOP, param);
                    TextInfo textInfo1 = new TextInfo();
                    textInfo1.setFontName(textInfo.getFontName());
                    textInfo1.setFontSize(textInfo.getFontSize());
                    textInfo1.setContent(content);
                    result.add(textInfo1);
                    content = "";
                }
                content += textInfo.getContent();
                textInfo.setContent("");
                result.add(textInfo);
            }
            content += textInfo.getContent();
            if(i==list.size()-1 && !StringUtils.isEmpty(content)){
                content = convertColor(content, op, logicOP, param);
                textInfo.setContent(content);
                textInfo.setFontName(fontName);
                textInfo.setFontSize(fontSize);
                result.add(textInfo);
            }
        }

        return result;
    }

    /****
     * 实现特殊字的颜色替换
     * @param origin
     * @param op
     * @param logicOP
     * @param param
     * @return
     */
    private static String convertColor(String origin, String op, String logicOP, String param){
        // 获取所有设备
        boolean flag = false;
        List<String> equip = getEquip(origin);
        if(origin.equals("参见1I10A")){
            return origin;
        }
        origin = origin.replace("<sup style='font-size: 1px'>3</sup>", "3");
        if((logicOP.equals("-") || logicOP.equals("～") || logicOP.equals("~")) && isOther(origin)){
            origin = origin.replace(logicOP, "<span style='background-color:"+ logicOpColor +"'>"+ logicOP +"</span>");
            origin = "<span style='background-color:"+ paramColor +"'>" + origin + "</span>";
            return origin;
        }

        // 逻辑
        if(!StringUtils.isEmpty(logicOP)){
            origin = origin.replace("<br />", "_?_");
            if(logicOP.equals(">") || logicOP.equals("<") || logicOP.equals("＞") || logicOP.equals("＜")){
                flag = true;
            }

            String logicRep = "<span style='background-color:"+ logicOpColor +"'>" + logicOP + "</span>";
            origin = origin.replace(logicOP, logicRep);
            origin = origin.replace("&lt;", "<span style='background-color:"+ logicOpColor +"'>&lt;</span>");
            origin = origin.replace("&gt;", "<span style='background-color:"+ logicOpColor +"'>&gt;</span>");
            if (flag){
                if(origin.contains("（")){
                    int index = origin.indexOf("（");
                    String after = origin.substring(index);
                    String pre = origin.substring(0, index);
                    origin = "<span style='background-color:"+ paramColor +"'>" + pre + "</span>" + after;
                }else{
                    origin = "<span style='background-color:"+ paramColor +"'>" + origin + "</span>";
                }

                origin = origin.replace("_?_", "<br />");
                origin = origin.replace("m3", "m<sup style='font-size:1px'>3</sup>");
                return origin;
            }

            origin = origin.replace("_?_", "<br />");
        }
        // 操作
        if(!StringUtils.isEmpty(op)){
            String opRep = "<span style='background-color:"+ opColor +"'>" + op + "</span>";
            origin = origin.replace(op, opRep);
        }
        // 参数
        String paramUnit = getParamUnit(param);
        if(!StringUtils.isEmpty(paramUnit)){
            origin = origin.replace("&ensp;", "_?_");
            List<String> paramNum = getParamNum(origin, paramUnit);

            if(paramNum.size()>0){
                for (String str:paramNum){
                    String paramRep = "<span style='background-color:"+ paramColor +"'>" + str + "</span>";
                    origin = origin.replace(str,paramRep);
                }
            }

            origin = origin.replace("_?_", "&ensp;");
        }

        // 设备
        if(equip.size()>0){
            for (String str: equip){
                String equRep = "<span style='background-color:"+ equipColor +"'>" + str + "</span>";
                origin = origin.replace(str, equRep);
            }
        }
        // 特殊数据的处理
        origin = getOther(origin);
        // 处理物理量
        // 获取字典表对象
        SysDictDataMapper sysDictDataMapper = SpringUtils.getBean("sysDictDataMapper");
        // 获取操作数据
        List<SysDictData> doc_phyQua = sysDictDataMapper.selectDictDataByType("doc_phyQua");
        List<String> listStr = new ArrayList<>();
        for (SysDictData s:doc_phyQua){
            listStr.add(s.getDictValue());
        }
        for (String str: listStr){
            String equRep = "<span style='background-color:"+ paramColor +"'>" + str + "</span>";
            origin = origin.replace(str, equRep);
        }
        if(flag){
            origin = getOtherColor(origin);
        }

        return origin;
    }

    /*************************** 工具方法 ****************************/
    /***
     * 处理特殊类型的参数
     * @param msg
     * @return
     */
    private static boolean isOther(String msg){
        msg = msg.replace("<br />", "");
        String reg = "^\\d+\\.?\\d+-\\d+\\.?\\d+[A-z.]*|\\d+\\.?\\d+～\\d+\\.?\\d+[A-z.]*|$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);

        return matcher.matches();
    }

    /***
     * 获取参数中的单位
     * @param msg
     * @return
     */
    private static String getParamUnit(String msg){
        for (int i=0; i<msg.length(); i++){
            char charAt = msg.charAt(i);
            if(!DocHandleBusiness.isNum(charAt) && charAt!='.' && charAt!=' ' && charAt!='-'){
                return msg.substring(i);
            }
        }
        return "";
    }
    /***
     * 处理特殊需要加标签的位置
     * @param msg
     * @return
     */
    private static String getOther(String msg){
        msg = msg.replace("m3", "m<sup style='font-size:1px'>3</sup>");
        msg = msg.replace("报警值", "<span style='background-color:"+ logicOpColor +"'>报警值</span>");
        msg = msg.replace("跳闸值", "<span style='background-color:"+ logicOpColor +"'>跳闸值</span>");
        msg = msg.replace("12bar.a，188℃", "<span style='background-color:"+ paramColor +"'>12bar.a，188℃</span>");
        msg = msg.replace("64.3bar，280.1℃", "<span style='background-color:"+ paramColor +"'>64.3bar，280.1℃</span>");
        msg = msg.replace("280±30mm", "<span style='background-color:"+ paramColor +"'>280±30mm</span>");
        
        return msg;
    }

    /***
     * 处理物理量  V>1056m3
     * @param msg
     * @return
     */
    private static String getOtherColor(String msg){

        msg = msg.replace("V", "<span style='background-color:"+ paramColor +"'>V</span>");
        msg = msg.replace("P", "<span style='background-color:"+ paramColor +"'>P</span>");

        return msg;
    }

    /***
     * 获取所有需要标记的参数
     * @param msg
     * @return
     */
    private static List<String> getParamNum(String msg, String unit){
        List<String> list = new ArrayList<>();
        String reg = "-?\\d+\\.?\\d*\\s*" + "\\_?\\??\\_?" + unit;
        Pattern pattern = Pattern.compile(reg);
        boolean flag = true;
        while (flag){
            Matcher matcher = pattern.matcher(msg);
            if(matcher.find()){
                String group = matcher.group();
                list.add(group);
                msg = msg.substring(msg.indexOf(group)+group.length());
            }else{
                flag = false;
            }
        }

        return list;
    }

    /***
     * 获取数字
     * @param msg
     * @return
     */
    private static List<String> getNum(String msg){
        List<String> list = new ArrayList<>();
        String reg = "\\d+\\.?\\d*\\s*";
        Pattern pattern = Pattern.compile(reg);
        //Matcher matcher = pattern.matcher(msg);
        boolean flag = true;
        while (flag){
            Matcher matcher = pattern.matcher(msg);
            if(matcher.find()){
                String group = matcher.group();
                list.add(group);
                msg = msg.substring(msg.indexOf(group)+group.length());
            }else{
                flag = false;
            }
        }
        return list;
    }
}
